package com.example.controller;

import com.example.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
/*
* @ResponseBody 将方法返回值直接响应，如果返回值类型是实体对象/集合，将会转换为JSON格式响应
* @RestController = @Controller + @ResponseBody
* */
@RestController
public class RequestController {
/*    @RequestMapping("/simpleParam")
    public String simpleParam(HttpServletRequest request) {
        String name = request.getParameter("name");
        String ageStr = request.getParameter("age");

        int age = Integer.parseInt(ageStr);
        System.out.println(name+":"+age);
        return "ok";
    }*/

    @RequestMapping("/simpleParam")

/*    * 如果方法形参名称与请求参数名称不匹配，可以使用@RequestParam完成映射，其中的required默认是false必须传递字段
    * */
    public String simplePara(@RequestParam(name="name") String name, Integer age) {
        System.out.println(name+":"+age);
        return "Hello "+name+":"+age;
    }
/*
* 实体类参数
* */
    @RequestMapping("/simplePojo")
    public String simplePojo(User user) {
        System.out.println(user);
        return "ok";
    }
    /*
    * 数组参数
    * */
    @RequestMapping("/simpleArray")
    public String simpleArray(String[] hobby){
        System.out.println(Arrays.toString(hobby));
        return "ok";
    }

    /*
     * 集合参数
     * */
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobby){
        System.out.println(hobby);
        return "ok";
    }

    /*
     * 日期时间类
     * */
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")LocalDateTime updateTime){
        System.out.println(updateTime);
        return "ok";
    }
    /*
     * json
     * */
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user){
        System.out.println(user);
        return "ok";
    }

    /*
    * 路径参数
    * */
    @RequestMapping("/path/{id}")
    public String routeParam(@PathVariable Integer id){
        System.out.println(id);
        return "ok";
    }
}
